package SEG_ApplicationLayer;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import CommonApplicationLayer.*;
import SEG_BusinessLayer.SEG_ModuloBL;
import SEG_EntityLayer.SEG_ModuloEL;
import java.awt.Color;
import java.awt.Cursor;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;

public class FrmSEG_ControlPanel extends JFrame {
    
    //--------------------------------------------------------------------------
    // <editor-fold defaultstate="collapsed" desc="Variables">

        private short sysUsuario;
        private String sysLogin;

    // </editor-fold>
        
    //--------------------------------------------------------------------------
    // <editor-fold defaultstate="collapsed" desc="Seguridad">
        
        private void obtenerDatosSesion() 
        {
            sysUsuario = InfoComun.getInstancia().getIdUsuario();
            sysLogin = InfoComun.getInstancia().getLogin();
        }

    // </editor-fold>
        
    //--------------------------------------------------------------------------
    // <editor-fold defaultstate="collapsed" desc="Apariencia">
        
    // </editor-fold>
        
    //--------------------------------------------------------------------------
    // <editor-fold defaultstate="collapsed" desc="ControlEventos">
        
        public FrmSEG_ControlPanel() 
        {
            initComponents();
            this.setIconImage(new ImageIcon(getClass().getResource("/Resources/world.png")).getImage());
        }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        smuIniciaSesion = new javax.swing.JMenuItem();
        smuCierraSesion = new javax.swing.JMenuItem();
        smuCambio = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        smuSalir = new javax.swing.JMenuItem();
        smsError = new errorProvider.FErrorProvider();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        lblUsuario = new javax.swing.JLabel();
        cdtModulo = new pageTab.FDockingTab();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstModulo = new javax.swing.JList();
        lblOpcion = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mnuConexion = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mnuSalir = new javax.swing.JMenuItem();
        mnuAyuda = new javax.swing.JMenu();
        mnuAcerca = new javax.swing.JMenuItem();

        smuIniciaSesion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/connect.png"))); // NOI18N
        smuIniciaSesion.setText("Iniciar Sesión");
        smuIniciaSesion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                smuIniciaSesionActionPerformed(evt);
            }
        });
        jPopupMenu1.add(smuIniciaSesion);

        smuCierraSesion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/disconnect.png"))); // NOI18N
        smuCierraSesion.setText("Cerrar Sesión");
        smuCierraSesion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                smuCierraSesionActionPerformed(evt);
            }
        });
        jPopupMenu1.add(smuCierraSesion);

        smuCambio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/lock.png"))); // NOI18N
        smuCambio.setText("Cambiar Contraseña");
        smuCambio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                smuCambioActionPerformed(evt);
            }
        });
        jPopupMenu1.add(smuCambio);
        jPopupMenu1.add(jSeparator2);

        smuSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/door_out.png"))); // NOI18N
        smuSalir.setText("Salir");
        smuSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                smuSalirActionPerformed(evt);
            }
        });
        jPopupMenu1.add(smuSalir);

        smsError.setText("fErrorProvider1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/user.png"))); // NOI18N

        lblUsuario.setText("No Conectado");

        cdtModulo.setVisualStyle(3);

        lstModulo.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstModulo.setFixedCellHeight(26);
        lstModulo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstModuloMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(lstModulo);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                .addContainerGap())
        );

        cdtModulo.addTab(" Módulos ", jPanel2);

        lblOpcion.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblOpcion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/bullet_arrow_down.png"))); // NOI18N
        lblOpcion.setIconTextGap(0);
        lblOpcion.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblOpcionMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblOpcionMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblOpcionMouseExited(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cdtModulo, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblUsuario)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblOpcion, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblUsuario)
                        .addComponent(lblOpcion, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(cdtModulo, javax.swing.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
                .addContainerGap())
        );

        jMenu1.setText("Opciones");

        mnuConexion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/database_connect.png"))); // NOI18N
        mnuConexion.setText("Conexión");
        mnuConexion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuConexionActionPerformed(evt);
            }
        });
        jMenu1.add(mnuConexion);
        jMenu1.add(jSeparator1);

        mnuSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/door_out.png"))); // NOI18N
        mnuSalir.setText("Salir");
        mnuSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuSalirActionPerformed(evt);
            }
        });
        jMenu1.add(mnuSalir);

        jMenuBar1.add(jMenu1);

        mnuAyuda.setText("Ayuda");

        mnuAcerca.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/vcard.png"))); // NOI18N
        mnuAcerca.setText("Acerca de...");
        mnuAcerca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuAcercaActionPerformed(evt);
            }
        });
        mnuAyuda.add(mnuAcerca);

        jMenuBar1.add(mnuAyuda);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        cargarDatosIniciales();
        new FrmSEG_Login(this, true).setVisible(true);
        obtenerDatosSesion();
        if ( sysUsuario > 0 ) {
            setEstadoForm(true);
            setCursor(new Cursor(Cursor.WAIT_CURSOR));
            listarModulosxUsuario();
            setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
        else {
            setEstadoForm(false);
            lstModulo.setListData(new Object[0]);
        }
    }//GEN-LAST:event_formWindowOpened

    private void lblOpcionMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblOpcionMouseClicked
        jPopupMenu1.show(lblOpcion, 0, lblOpcion.getHeight()+2);
    }//GEN-LAST:event_lblOpcionMouseClicked

    private void lblOpcionMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblOpcionMouseEntered
        lblOpcion.setOpaque(true);
        lblOpcion.setBackground(new Color(240,240,240));
        lblOpcion.setBorder(BorderFactory.createLineBorder(new Color(190,190,190)));
        lblOpcion.setSize(16, 14);
    }//GEN-LAST:event_lblOpcionMouseEntered

    private void lblOpcionMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblOpcionMouseExited
        lblOpcion.setOpaque(false);
        lblOpcion.setBackground(new Color(255,255,255));
        lblOpcion.setBorder(null);
        lblOpcion.setSize(16, 16);
    }//GEN-LAST:event_lblOpcionMouseExited

    private void mnuAcercaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuAcercaActionPerformed
        new FrmSEG_AcercaDe(this, true).setVisible(true);
    }//GEN-LAST:event_mnuAcercaActionPerformed

    private void mnuSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuSalirActionPerformed
        dispose();
        System.exit(0);
    }//GEN-LAST:event_mnuSalirActionPerformed

    private void mnuConexionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuConexionActionPerformed
        new FrmSEG_ConfigurarConexion(this, true).setVisible(true);
    }//GEN-LAST:event_mnuConexionActionPerformed

    private void smuIniciaSesionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_smuIniciaSesionActionPerformed
        new FrmSEG_Login(this, true).setVisible(true);
        obtenerDatosSesion();
        if ( sysUsuario > 0 ) {
            setEstadoForm(true);
        }
        else {
            setEstadoForm(false);
        }
        setCursor(new Cursor(Cursor.WAIT_CURSOR));
        listarModulosxUsuario();
        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_smuIniciaSesionActionPerformed

    private void smuCierraSesionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_smuCierraSesionActionPerformed
        InfoComun.getInstancia().setIdEmpresa((byte)0);
        InfoComun.getInstancia().setIdUsuario((short) 0);
        InfoComun.getInstancia().setLogin(null);
        InfoComun.getInstancia().setNombres(null);
        obtenerDatosSesion();
        FrmSEG_Principal.getInstancia().dispose();
        setEstadoForm(false);
        lstModulo.setListData(new Object[0]);
    }//GEN-LAST:event_smuCierraSesionActionPerformed

    private void smuCambioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_smuCambioActionPerformed
        
    }//GEN-LAST:event_smuCambioActionPerformed

    private void smuSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_smuSalirActionPerformed
        dispose();
        System.exit(0);
    }//GEN-LAST:event_smuSalirActionPerformed

    private void lstModuloMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstModuloMouseClicked
        if ( evt.getClickCount() == 2 && !lstModulo.isSelectionEmpty() ) {
            SEG_ModuloEL modulo = (SEG_ModuloEL) lstModulo.getSelectedValue();
            
            if ( modulo.getModulo() > 0 ) {
                FrmSEG_Principal.getInstancia().cargarModulo(modulo.getModulo(),modulo.getDescripcion());
                FrmSEG_Principal.getInstancia().setExtendedState(JFrame.MAXIMIZED_BOTH);
                FrmSEG_Principal.getInstancia().setVisible(true);
            }
            
        }
    }//GEN-LAST:event_lstModuloMouseClicked

        public static void main(String args[]) 
        {
            try 
            {
                for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) 
                {
                    if ("Windows".equals(info.getName())) 
                    {
                        UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } 
            catch (Exception ex) 
            {
                JOptionPane.showMessageDialog(null, ex.getMessage(), SMSComun.smsEmpresa, JOptionPane.ERROR_MESSAGE);
            }
            finally
            {
                new FrmSEG_ControlPanel().setVisible(true);
            }
        }
        
    // </editor-fold>
        
    //--------------------------------------------------------------------------
    // <editor-fold defaultstate="collapsed" desc="Funcionalidad">
        
        private void cargarDatosIniciales() {
            setTitle(SMSComun.smsEmpresa);
        }
        
        private void setEstadoForm(boolean estado) {
            if ( estado ) {
                lblUsuario.setText(sysLogin.substring(0, 1).toUpperCase().concat(sysLogin.substring(1)));
            }
            else {
                lblUsuario.setText("No Conectado");
            }
            smuIniciaSesion.setEnabled(!estado);
            smuCierraSesion.setEnabled(estado);
            smuCambio.setEnabled(estado);
        }
        
    // </editor-fold>
        
    //--------------------------------------------------------------------------
    // <editor-fold defaultstate="collapsed" desc="AccesoDatos">
        
        private void listarModulosxUsuario() {
            try {
                List<SEG_ModuloEL> lista = SEG_ModuloBL.getInstancia().ListarModuloxUsuario(sysUsuario);
                
                lstModulo.setListData(lista.toArray());
                lstModulo.setCellRenderer(new RenderListaModulo());
            } catch( Exception ex ){
                JOptionPane.showMessageDialog(this, ex.getMessage(), SMSComun.smsEmpresa, JOptionPane.ERROR_MESSAGE);
            }
        }
        
    // </editor-fold>
        
    //--------------------------------------------------------------------------
    // <editor-fold defaultstate="collapsed" desc="Validacion">
        
    // </editor-fold>
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private pageTab.FDockingTab cdtModulo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JLabel lblOpcion;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JList lstModulo;
    private javax.swing.JMenuItem mnuAcerca;
    private javax.swing.JMenu mnuAyuda;
    private javax.swing.JMenuItem mnuConexion;
    private javax.swing.JMenuItem mnuSalir;
    private errorProvider.FErrorProvider smsError;
    private javax.swing.JMenuItem smuCambio;
    private javax.swing.JMenuItem smuCierraSesion;
    private javax.swing.JMenuItem smuIniciaSesion;
    private javax.swing.JMenuItem smuSalir;
    // End of variables declaration//GEN-END:variables
}
